home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK2.toast / Development Kits (Disc 2) / Open Transport / OT 1.1 GM / Open Transport SDK / Open Tpt Client Developer / Samples / Internet / OTTcpPitchByNameSample.cp < prev    next >
Encoding:
Text File  |  1996-02-13  |  10.1 KB  |  449 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OTTcpPitchByNameSample.cp
  3.  
  4.     Contains:    Tcp pitch sample.
  5.  
  6.     Copyright:    © 1993-1995 by Apple Computer, Inc., all rights reserved.
  7.  
  8. */
  9.  
  10.  
  11. // OT TCP Pitch Test Program Connecting By Name Rather Than By Address (as an SIOW app)
  12.  
  13. #include <QuickDraw.h>
  14. #include <stdio.h>
  15. #include <StdLib.h>
  16. #include <TextUtils.h>
  17. #include <strings.h>
  18. #include <String.h>
  19. #include <Events.h>
  20. #include <Desk.h>
  21. #include <OpenTransport.h>
  22. #include <OpenTptInternet.h>
  23.  
  24. /*******************************************************************************
  25. ** GLOBAL VARIABLES
  26. ********************************************************************************/
  27.  
  28. #define kMaxDataLen 256
  29.  
  30. const size_t    kMyPoolSize = 60000;
  31.  
  32. InetPort      gPitchPort        = 0;
  33. InetHost    gPitchIpAddr    = 0;
  34.  
  35. unsigned short gBindCompleted        = 0;
  36. unsigned short gCallRcvOrdDiscon    = 0;
  37.  
  38. char theAddr[257];
  39. DNSAddress* hisAddr = (DNSAddress*)theAddr;
  40.  
  41.  
  42. struct InetAddress sndsin, rcvsin, reqsin, retsin;
  43. char data[kMaxDataLen];
  44. char defaultData[] = "Go Cal, beat Stanford !!!";
  45.  
  46. /*******************************************************************************
  47. ** Function Prototypes
  48. ********************************************************************************/
  49.  
  50. void Inits();
  51. void CleanUp();
  52. void Idle();    
  53. void DoIt();
  54.  
  55.  
  56. /*******************************************************************************
  57. **  main function
  58. ********************************************************************************/
  59.  
  60. void main()
  61. {
  62.     Inits();
  63.     DoIt();
  64.     CleanUp();
  65. }
  66.  
  67. /*******************************************************************************
  68. ** Initialize Quickdraw and ASLM
  69. ********************************************************************************/
  70.  
  71. void Inits()
  72. {
  73.     InitGraf(&qd.thePort);
  74.     if (InitOpenTransport() != kOTNoError)
  75.     {
  76.         fprintf(stderr, "OTTcpPitch: Could not initialize ASLM, exiting\n");
  77.         exit(1);
  78.     }
  79. }
  80.  
  81. /*******************************************************************************
  82. ** ShowEndpointOptions
  83. ********************************************************************************/
  84. void ShowEndpointOptions(TEndpoint*    ep)
  85. {
  86.     /*
  87.      *    This function retrieves and displays 
  88.      *    the IP and TCPP endpoint options for this endpoint.
  89.      */
  90.      
  91.     OSStatus    err;
  92.     TOptMgmt*            ret = (TOptMgmt*)OTAlloc(ep, T_OPTMGMT, T_OPT, &err);
  93.  
  94.     
  95.     do
  96.     {
  97.         fprintf(stderr, "Current Readable IP Option Settings for endpoint @ %08lX:\n", ep);
  98.         if ( ret == NULL )
  99.         {
  100.             fprintf(stderr, "ERROR: could not allocate TOptMgmt structure (%d)\n", err);
  101.             break;
  102.         }
  103.         //
  104.         // Get the current IP options
  105.         //
  106.         TOptMgmt        req;
  107.         TOptionHeader    option;
  108.         
  109.         option.len        = kOTOptionHeaderSize;
  110.         option.level    = INET_IP;
  111.         option.name        = T_ALLOPT;
  112.         
  113.         req.opt.buf = (UInt8*)&option;
  114.         req.opt.len    = kOTOptionHeaderSize;
  115.         req.flags    = T_CURRENT;
  116.         
  117.         err = ep->OptionManagement(&req, ret);
  118.         if ( err != kOTNoError )
  119.         {
  120.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  121.             break;
  122.         }
  123.         //
  124.         // Now, let's print the options
  125.         //
  126.         {
  127.             TOption*    opt = (TOption*)ret->opt.buf;
  128.             char        string[512];
  129.  
  130.             err = OTCreateOptionString("ip", &opt, ret->opt.buf + ret->opt.len,
  131.                                        string, sizeof(string));
  132.             
  133.             if ( err == kOTNoError )
  134.             {
  135.                 char*    str = string;
  136.                 size_t    len = 0;
  137.                 while ( true )
  138.                 {
  139.                     char* temp = strchr(str, ',');
  140.                     if ( temp == NULL )
  141.                     {
  142.                         fprintf(stderr, "%s\n", str);
  143.                         break;
  144.                     }
  145.                     if ( len + temp - str + 1 > 80 )
  146.                     {
  147.                         fprintf(stderr, "\n");
  148.                         if ( *str == ' ' )
  149.                             str += 1;
  150.                         len = 0;
  151.                     }
  152.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  153.                     len += temp - str + 1;
  154.                     str = temp + 1;
  155.                 }
  156.             }
  157.         }
  158.         
  159.         TOption* opt =  OTFindOption(ret->opt.buf, ret->opt.len, INET_IP, IP_TTL);
  160.             
  161.         if ( opt == NULL )
  162.             fprintf(stderr, "ERROR:OptionManagement did not have IP_TTL in returned options\n");
  163.  
  164.         //
  165.         // Get the current TCP options
  166.         //
  167.         fprintf(stderr, "Current Readable TCP Option Settings for endpoint @ %08lX:\n", ep);
  168.         option.len        = kOTOptionHeaderSize;
  169.         option.level    = INET_TCP;
  170.         option.name        = T_ALLOPT;
  171.         
  172.         req.opt.buf = (UInt8*)&option;
  173.         req.opt.len    = kOTOptionHeaderSize;
  174.         req.flags    = T_CURRENT;
  175.         
  176.         err = ep->OptionManagement(&req, ret);
  177.         if ( err != kOTNoError )
  178.         {
  179.             fprintf(stderr, "ERROR: OptionManagement T_CURRENT request returned %d\n", err);
  180.             break;
  181.         }
  182.         //
  183.         // Now, let's print the options
  184.         //
  185.         {
  186.             TOption*    opt = (TOption*)ret->opt.buf;
  187.             char        string[512];
  188.  
  189.             err = OTCreateOptionString("tcp", &opt, ret->opt.buf + ret->opt.len,
  190.                                        string, sizeof(string));
  191.             
  192.             if ( err == kOTNoError )
  193.             {
  194.                 char*    str = string;
  195.                 size_t    len = 0;
  196.                 while ( true )
  197.                 {
  198.                     char* temp = strchr(str, ',');
  199.                     if ( temp == NULL )
  200.                     {
  201.                         fprintf(stderr, "%s\n", str);
  202.                         break;
  203.                     }
  204.                     if ( len + temp - str + 1 > 80 )
  205.                     {
  206.                         fprintf(stderr, "\n");
  207.                         if ( *str == ' ' )
  208.                             str += 1;
  209.                         len = 0;
  210.                     }
  211.                     fprintf(stderr, "%*.*s", temp - str + 1, temp - str + 1, str);
  212.                     len += temp - str + 1;
  213.                     str = temp + 1;
  214.                 }
  215.             }
  216.             fprintf(stderr, "\n");
  217.         }
  218.         
  219.     } while ( false );
  220.     OTFree(ret, T_OPTMGMT);
  221. }
  222.  
  223.  
  224. /*******************************************************************************
  225. ** Clean up at the end
  226. ********************************************************************************/
  227.  
  228. void CleanUp()
  229. {
  230.     CloseOpenTransport();
  231. }
  232.  
  233. /*******************************************************************************
  234. ** Idle
  235. ********************************************************************************/
  236.  
  237. void Idle()
  238. {
  239.     SystemTask();
  240. }
  241.  
  242. /*******************************************************************************
  243. ** EventHandler
  244. ********************************************************************************/
  245.  
  246. pascal void EventHandler(void*, OTEventCode event, OTResult /* result */, void*)
  247. {
  248.  
  249.     switch ( event )
  250.     {
  251.         case T_BINDCOMPLETE:
  252.                             gBindCompleted = 1;
  253.                             break;
  254.         case T_ORDREL:
  255.                             gCallRcvOrdDiscon = 1;
  256.                             break;
  257.         default:
  258.                             DebugStr("\pEventHandler got unexpected event");
  259.                             break;
  260.     }
  261.     return;
  262. }
  263.  
  264. /*******************************************************************************
  265. ** DoIt
  266. ********************************************************************************/
  267.  
  268. void DoIt()
  269. {
  270.     TEndpoint*        ep = NULL;
  271.     TEndpointInfo    info;
  272.     TBind            req, ret;
  273.     TCall            sndcall, rcvcall;
  274.     OSStatus        err = kOTNoError;
  275.     long            myport = 0;
  276.     InetHost        myaddr = 0;
  277.     char            mystr[255];
  278.     long            bytes = 0;
  279.     OTFlags            flags = 0;
  280.  
  281.     myport = 0;
  282.     fprintf(stderr, "Enter local TCP port number\n");
  283.     if (gets(mystr) != 0) 
  284.     {
  285.         stringtonum(mystr, &myport);
  286.         gPitchPort =(InetPort)  myport;
  287.     }
  288.     myaddr = 0;
  289.     fprintf(stderr, "Enter local IP address\n");
  290.     if (gets(mystr) != 0)
  291.     {
  292.         if (OTInetStringToHost(mystr, &myaddr) == 0)
  293.         {
  294.             gPitchIpAddr = (InetHost) myaddr;
  295.         }
  296.     }
  297.     fprintf(stderr, "Enter the target as name:port\n");
  298.     if ( gets(hisAddr->fName) != 0 )
  299.     {
  300.         hisAddr->fAddressType = kOTGenericName;
  301.     }
  302.  
  303.     fprintf(stderr, "What should I send ? (enter data string)\n");
  304.     if ( gets(data) == 0 )
  305.     {
  306.         strcpy(data, defaultData);
  307.         fprintf(stderr, "sending default data: <%s>\n", data);
  308.     }
  309.     data[strlen(data)] = '\n';
  310.  
  311.     fprintf(stderr, "The program will send a packet to <%s> \n", hisAddr->fName);
  312.  
  313.     memset(&sndsin, 0, sizeof(struct InetAddress));
  314.     memset(&rcvsin, 0, sizeof(struct InetAddress));
  315.     memset(&sndcall, 0, sizeof(TCall));
  316.     memset(&rcvcall, 0, sizeof(TCall));
  317.     memset(&reqsin, 0, sizeof(struct InetAddress));
  318.     memset(&retsin, 0, sizeof(struct InetAddress));
  319.     memset(&req, 0, sizeof(TBind));
  320.     memset(&ret, 0, sizeof(TBind));
  321.  
  322.     do
  323.     {
  324.         //
  325.         // Now create a TCP
  326.         //
  327.         ep = OTOpenEndpoint(OTCreateConfiguration(kTCPName), 0, &info, &err);
  328.  
  329.         if ( ep == NULL || err != kOTNoError )
  330.         {
  331.             ep = NULL;
  332.             fprintf(stderr,"ERROR: OpenEndpoint(\"TCP\") failed with %d\n", err);
  333.             break;
  334.         }
  335.  
  336.         err = ep->SetSynchronous();
  337.         if ( err != kOTNoError )
  338.         {
  339.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  340.             break;
  341.         }
  342.  
  343.         //
  344.         // Install notifier we're going to use for testing
  345.         //
  346.         err = ep->InstallNotifier(&EventHandler, 0);
  347.         if ( err != kOTNoError )
  348.         {
  349.             fprintf(stderr, "ERROR: InstallNotifier() failed with %d\n", err);
  350.             break;
  351.         }
  352.  
  353.         //
  354.         // Try to bind
  355.         // 
  356.         OTInitInetAddress(&reqsin, gPitchPort, gPitchIpAddr);
  357.  
  358.         req.addr.len = sizeof(struct InetAddress);
  359.         req.addr.buf = (unsigned char *) &reqsin;
  360.         req.qlen = 2;                                        // don't care for tcp
  361.         ret.addr.maxlen = sizeof(struct InetAddress);
  362.         ret.addr.buf = (unsigned char *) &retsin;
  363.  
  364.         // bind TCP to current address and port
  365.         err = ep->Bind(&req, &ret);
  366.         if ( err != kOTNoError )
  367.         {
  368.             fprintf(stderr, "ERROR: Bind() failed with %d\n", err);
  369.             break;
  370.         }
  371.         err = ep->SetSynchronous();
  372.         if ( err != kOTNoError )
  373.         {
  374.             fprintf(stderr, "ERROR: SetSynchronous() failed with %d\n", err);
  375.             break;
  376.         }
  377.         
  378.         ShowEndpointOptions(ep);
  379.  
  380.         sndcall.addr.len = sizeof(OTAddressType) + strlen(hisAddr->fName);
  381.         sndcall.addr.buf = (UInt8*)hisAddr;
  382.     
  383.         rcvcall.addr.maxlen = sizeof(struct InetAddress);
  384.         rcvcall.addr.buf = (unsigned char *) &rcvsin;
  385.  
  386.         err = ep->Connect(&sndcall, &rcvcall);
  387.         if ( err != kOTNoError )
  388.         {
  389.             fprintf(stderr, "ERROR: Connect() failed with %d\n", err);
  390.             break;
  391.         }
  392.  
  393.         bytes = ep->Snd(data, strlen(data), flags);
  394.         if ( bytes >= 0 )
  395.         {
  396.             fprintf(stderr, "Sent packet\nbytes: <%d> data: %s", bytes, data);
  397.         }
  398.         else 
  399.         {
  400.             fprintf(stderr, "ERROR: Snd() failed with %d\n", bytes);
  401.         }
  402.         Idle();
  403.     } while (false);
  404.  
  405.     if ( ep != NULL )
  406.     {
  407.         err = ep->SndOrderlyDisconnect();
  408.         if ( err != kOTNoError )
  409.         {
  410.             if ( err == kOTLookErr )
  411.                 fprintf(stderr, "SndOrderlyDisconnect() returns %d\n", err);
  412.             else
  413.                 fprintf(stderr, "ERROR: SndOrderlyDisconnect() failed with %d\n", err);
  414.         }
  415.     
  416.         while (gCallRcvOrdDiscon == 0)
  417.             Idle();
  418.         err = ep->RcvOrderlyDisconnect();
  419.         if ( err != kOTNoError )
  420.         {
  421.             fprintf(stderr, "ERROR: RcvOrderlyDisconnect() failed with %d\n", err);
  422.         }
  423.     
  424.         //
  425.         // Remove notifier
  426.         //
  427.         ep->RemoveNotifier();
  428.         //
  429.         // Try to Unbind
  430.         // 
  431.     /*
  432.         err = ep->Unbind();
  433.         if ( err != kOTNoError )
  434.         {
  435.             fprintf(stderr, "ERROR: Unbind() returned %d\n", err);
  436.         }
  437.     */
  438.         //
  439.         // Get rid of endpoint.
  440.         //
  441.         err = OTCloseProvider(ep);
  442.         if ( err != kOTNoError )
  443.         {
  444.             fprintf(stderr, "ERROR: CloseEndpoint() failed with %d\n", err);
  445.         }
  446.     }
  447.     fprintf(stderr, "Bye\n");
  448. }
  449.